<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" label-width="120px" :rules="rules" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-row>
            <el-col :span="12" >
              <el-form-item  label="仓库号:"   :required="true" style="width:600px" prop="storageId" >
                <el-select v-model="postForm.storageId" placeholder="请选择一级仓库">
                  <el-option :label="item.shortName" :value="item.id" v-for="(item,index) of storageListArr" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" ><!-- 商户号 custom_id-->
              <el-form-item  label="商户号:"   :required="true"  prop="customerId" >
                <el-select v-model="postForm.customerId" placeholder="请选择商家">
                  <el-option :label="item.customerName" :value="item.id" v-for="(item,index) of customListArr" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" ><!-- `dtuId` varchar(255) DEFAULT  二级仓库号 -->
              <el-form-item label="DTU号:"   :required="true"  prop="dtuId">
                <el-select v-model="postForm.dtuId" placeholder="请选择DTU">
                  <el-option :label="item.dtuName" :value="item.id" v-for="(item,index) of dtuListArr" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" ><!-- 设备类型`type` tinyint(1) DEFAULT '1' 类型:1单房间 W10 型,2多房间 W40型 -->
              <el-form-item label="设备类型:" :required="true"  prop="type">
                <el-select v-model="postForm.type" placeholder="请选择DTU">
                  <el-option :label="item" :value="index" v-for="(item,index) of meterTypeArr" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-col :span="12" >
            <el-form-item  label="设备名称:"   :required="true"  prop="meterName" >
              <el-input v-model="postForm.meterName"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 设备编码:"   :required="true"  prop="meterCode" >
              <el-input  v-model="postForm.meterCode"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item  label="设备型号:"  prop="model"  :required="true"  >
              <el-input v-model="postForm.model"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item label="设备类型:">
              <el-select v-model="postForm.type" placeholder="请选择DTU">
                <el-option :label="item" :value="index" v-for="(item,index) of meterTypeArr" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="状态:">
              <el-switch
                v-model="postForm.status"
                active-text="可用"
                inactive-text="禁用"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="12" style="padding-left:60px;">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
          <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
        </el-col>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { userSearch } from '@/api/remoteSearch'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'
import {meterDetail, updateMeter} from "../../../api/meter";
import { dtulist }     from "../../../api/dtu"
import { customerList }     from "../../../api/customer"
import { storageList }   from  '@/api/storage'
const defaultForm = {
  id: "",
  name: "",
  code: "",
  model: "",
  type: "",
  status: "",
  desc: ""
}

export default {
  name: 'UnitEdit',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  filters: {
    statusName(status) {
      if (status==1) {
        return '可用';
      }else{
        return '禁用'
      }
    }
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        if(rule.field == 'dtuId'){
          callback(new Error('DTU必选！'))
        }
        if(rule.field == 'customerId'){
          callback(new Error('商家id必选！'))
        }
        if(rule.field == 'storageId'){
          callback(new Error('仓库id为必选！'))
        }
        if(rule.field == 'type'){
          callback(new Error('设备类型为必选！'))
        }
        if(rule.field == 'model'){
          callback(new Error('设备型号为必填!'))
        }
        if(rule.field == 'meterName'){
          callback(new Error('设备名称为必填!'))
        }
        if(rule.field == 'meterCode'){
          callback(new Error('设备编码为必填!'))
        }
      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      storageListArr:[] ,
      dtuListArr:[], //dtu列表
      meterTypeArr: [], //类型数组
      customListArr:[],
      editForm:{
        id: "",
        dtuId : '' ,
        customerId : '' ,
        storageId : '' ,
        meterName : "",
        meterCode : "",
        model : "",
        type : "",
        status : "" ,
        desc : "" ,
        updateTime : '',
        createTime : ''
      },
      loading: false,
      userListOptions: [],
      title:'',
      rules: {
        dtuId:   [{ validator: validateRequire }],
        customerId:   [{ validator: validateRequire }],
        storageId:   [{ validator: validateRequire }],
        type:  [{ validator: validateRequire }],
        model:  [{ validator: validateRequire }],
        customerName: [{ validator: validateRequire }],
        customerCode: [{ validator: validateRequire }]
      }
    }
  },
  computed: {
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.query && this.$route.query.id
      this.MeterDetail( id )
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
    dtulist( {"rid":1,"page":1, "limit":20, "status":0}).then(response => {
      this.dtuListArr = response.data.data
    })
    storageList( {"uissssd":1269,"page":1, "limit":2000, "status":0} ).then(response => {
      this.storageListArr = response.data.data
    })
    customerList( {"uissssd":1269,"page":1, "limit":20, "status":0} ).then(response => {
      this.customListArr = response.data.data
    })
    this.meterTypeArr[0] = '请选择类型'
    this.meterTypeArr[1] = '单房间 W10型'
    this.meterTypeArr[2] = '多房间 W40型'
  },
  methods: {
    MeterDetail(id) {
      meterDetail({id:id}).then(response => {
        this.postForm = response.data.data
        this.title += '  控制器 Id:${this.postForm.id}'
        console.log(this.postForm)
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    doSubmitForm() {
      this.editForm.id = this.postForm.id
      this.editForm.name = this.postForm.meterName
      this.editForm.code = this.postForm.meterCode
      this.editForm.model = this.postForm.model
      this.editForm.desc = this.postForm.desc
      this.editForm.type = this.postForm.type
      this.editForm.status = this.postForm.status
      updateMeter(this.editForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
